# 角色模型定义
from backend.infrastructure.database import db

class Role(db.Model):
    __tablename__ = 'roles'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(50), unique=True, nullable=False, comment="角色名")
    description = db.Column(db.String(255), nullable=True, comment="角色描述")
    status = db.Column(db.String(20), nullable=False, default="active", comment="状态")
    
    # 关联关系
    permissions = db.relationship(
        'Permission', 
        secondary='role_permissions', 
        backref='roles',
        lazy='dynamic'
    )
    
    def to_dict(self):
        return {
            'id': self.id,
            'name': self.name,
            'description': self.description,
            'status': self.status
        }
